<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/3 0003
 * Time: 14:31
 */

//留言板
include 'publics.php';
//留言内容查询
$sql = "select * from message where sn = 1 and put_user_id = ".$_SESSION['user']['u_id']." or get_user_id =".$_SESSION['user']['u_id'];
$data = query($sql);


//重组数组，将发送人和接收人装入
foreach($data as $j => $k){
    $aql1 = "select username from user where u_id = ".$k['put_user_id'];
    $user1 = find($aql1);
    $data[$j]['put_username'] = $user1['username'];

    $aql2 = "select username from user where u_id = ".$k['get_user_id'];
    $user2 = find($aql2);
    $data[$j]['get_username'] = $user2['username'];
}



//查询全部用户
$sql2 = "select * from user where sn = 1";
$user =  query($sql2);




//提交新留言
if(!empty($_POST)){
    $text = $_POST['text'];
    $put_user_id = $_SESSION['user']['u_id'];
    $get_user_id = $_POST['get_user_id'];
    $time = time();
    $int = "insert into message (put_user_id,get_user_id,text,crete_time) VALUES ($put_user_id,$get_user_id,'".$text."',$time)";
    $ints = insert($int);
    if($ints == true){
        echo '<script type="text/javascript">';
        echo 'alert("发送成功，三秒后返回")';
        echo '</script>';
        header('Refresh: 3; url=message.php');
        exit;
    }else{
        echo '<script type="text/javascript">';
        echo 'alert("发送失败 ，三秒后返回")';
        echo '</script>';
        header('Refresh: 3; url=message.php');
        exit;
    }
}

if(isset($_GET['m_id'])){
    $m_id = $_GET['m_id'];
    $sde = "delete from message where m_id = $m_id";
    $data = insert($sde);
    if($data == true){
        heads("删除成功 ，三秒后返回",3,'message.php');
    }else{
        heads("删除失败 ，三秒后返回",3,'message.php');
    }
}
function heads($das,$num,$url){
    echo '<script type="text/javascript">';
    echo 'alert("'.$das.'")';
    echo '</script>';
    header('Refresh: '.$num.'; url='.$url.'');
    exit;
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>留言板</title>
    <script src="jquery-1.10.1.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="./kkkk/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="./kkkk/ueditor.all.min.js"> </script>
    <!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
    <script type="text/javascript" charset="utf-8" src="./kkkk/lang/zh-cn/zh-cn.js"></script>

    <style type="text/css">
        div{
            width:100%;
        }
    </style>
</head>
<body>

<?php foreach($data as $k => $v){ ?>
<div class="top_all">
        <div style="width: 200px;color: blue"><span>发送人：</span><?php echo $v['put_username']?></div>
        <div style="width: 200px;color: green"><span>接收人：</span><?php echo $v['get_username']?></div>
        <div style="width: 500px;height:100px;color: blueviolet;border: 1px solid black"><?php echo $v['text']?></div>
        <div style="width: 400px;color: black"><span>时间：</span><?php echo date("Y-m-d H:i:s",$v['crete_time']); ?></div>
        <a href="message.php?m_id=<?php echo $v['m_id'] ?>">删除</a>
        <?php if($v['put_user_id'] == $_SESSION['user']['u_id'] ){ ?>
        <a href="update.php?m_id=<?php echo $v['m_id'] ?>">修改</a>
        <?php };?>
    </div>
<br>
<?php };?>

<!--查询出的全部用户，除去自己和冻结-->
<form action="message.php" method="post">
    <div>
        <script id="editor"name="text" type="text/plain" style="width:1024px;height:500px;">

        </script>
    </div>
    <select name="get_user_id" id="ss">
        <?php foreach($user as $h => $f){ ?>
            <?php if($f['u_id'] != $_SESSION['user']['u_id']){  ?>
                <option value="<?php echo $f['u_id']?>">
                    <?php echo $f['username']?>
                </option>
            <?php };?>
        <?php };?>
    </select>
    <input type="submit" value="发送">
</form>
<script type="text/javascript">
    var ue = UE.getEditor('editor');
//    function kk(){
//        var op = $('#text').val();
//        var sel = $('#ss').val();
//        console.log(sel);
//        return false;
//    }
</script>

</body>
</html>
